import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { BaseHttpService } from '../../../shared/services/baseHttp.service';
import { Observable } from 'rxjs/Observable';
import { StorageService } from '../../../shared/services/storage.service';
import { ChartPointModel } from '../../../shared/models/chartPoint.model';
import { ClusterModelWrapper } from '../riskCluster/cluster.model';
import { RiskListModel } from './models/riskList.model';
import { CompanyCountModel } from '../../../shared/models/companyCount.model';
import { RiskFilterModel } from './models/riskFilter.model';

/**
 * Overall risk rating main service
 * 
 * Author: Sorin Spiru
 * Date: 13.01.2018
 */
@Injectable()
export class ResearchRiskRatingService extends BaseHttpService {

    constructor(private http: HttpClient) {
        super();
    }

    /**
     * Get universe risk rating distribution
     */
    public getCompaniesBenchmark(): Observable<ChartPointModel[]> {
        return this.http.get<ChartPointModel[]>(this.serverUrl + 'riskratingserv/api/v1/companies/benchmark', {});
    }   

    /**
     * Get list of companies with risk rating research
     * @param filters Risk list filter object
     * @param skip Index to take from
     * @param take Number of companies to take
     * @param orderBy Name of column to order by
     * @param orderByAsc True to order ascending by the 'orderBy' column or false for descending
     */
    public getCompanies(filters: RiskFilterModel, skip: number, take: number, orderBy: string, orderByAsc: boolean): Observable<RiskListModel[]> {
        let params: HttpParams = new HttpParams();
        params = params.set("skip", skip.toString())
        .set("take", take.toString())
        .set('getCount', 'false')
        .set('orderBy', orderBy)
        .set('orderAscending', orderByAsc.toString());
        params = this.urlEncodeObject(filters, params, 'companyName');
        //because of the HppClient issue with '+' and probably other special chars, we set the parameter into the url
        return this.http.get<RiskListModel[]>(this.serverUrl + 'riskratingserv/api/v1/companies?companyName=' + this.paramEncode(filters, 'companyName'), { params: params });
    }

    /**
     * Get number of companies after the input filter is applied
     * @param filters Risk list filter object
     */
    public getCompaniesCount(filters: RiskFilterModel): Observable<CompanyCountModel> {
        let params: HttpParams = new HttpParams();
        params = params.set("getCount", 'true');
        params = this.urlEncodeObject(filters, params);
        //because of the HppClient issue with '+' and probably other special chars, we set the parameter into the url
        return this.http.get<CompanyCountModel>(this.serverUrl + 'riskratingserv/api/v1/companies?companyName=' + this.paramEncode(filters, 'companyName'), { params: params });
    }
}